/**
 * Filename: AgreementActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/12 14:10
 */
package com.github.postsanf.yinian.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.AgreementAdapter;
import com.github.postsanf.yinian.bean.YNAgreeItem;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.github.postsanf.yinian.utils.WrapContentLinearLayoutManager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户协议
 */
public class AgreementActivity extends BaseActivity implements ISkinUpdate {
    private Toolbar mToolbar;
    private RecyclerView rv_agreement;
    private TextView agree_tips_content;
    private View yn_agree_item_top;
    private AgreementAdapter mAdapter;
    private List<YNAgreeItem> mDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_agreement);
        BaseApplication.getInstance().mActivitys.add(this);
        initView();
    }

    private void initView() {
        initData();
        mToolbar = (Toolbar) findViewById(R.id.agreement_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        rv_agreement = (RecyclerView) findViewById(R.id.rv_agreement);
        WrapContentLinearLayoutManager linearLayoutManager = new WrapContentLinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_agreement.setLayoutManager(linearLayoutManager);
        mAdapter = new AgreementAdapter(mDatas);
        rv_agreement.setAdapter(mAdapter);
        yn_agree_item_top = View.inflate(this, R.layout.yn_agree_item_top, null);
        agree_tips_content = (TextView) yn_agree_item_top.findViewById(R.id.agree_tips_content);
        agree_tips_content.setText(getResources().getText(R.string.tips_content));
        mAdapter.setHeaderView(yn_agree_item_top);
        rv_agreement.setItemViewCacheSize(10);
    }

    private void initData() {
        mDatas.add(new YNAgreeItem(getResources().getText(R.string.agree_title_first).toString(), getResources().getText(R.string.agree_content_first).toString()));
        mDatas.add(new YNAgreeItem(getResources().getText(R.string.agree_title_second).toString(), getResources().getText(R.string.agree_content_second).toString()));
        mDatas.add(new YNAgreeItem(getResources().getText(R.string.agree_title_third).toString(), getResources().getText(R.string.agree_content_third).toString()));
        mDatas.add(new YNAgreeItem(getResources().getText(R.string.agree_title_fourth).toString(), getResources().getText(R.string.agree_content_fourth).toString()));
        mDatas.add(new YNAgreeItem(getResources().getText(R.string.agree_title_fifth).toString(), getResources().getText(R.string.agree_content_fifth).toString()));
        mDatas.add(new YNAgreeItem(getResources().getText(R.string.agree_title_sixth).toString(), getResources().getText(R.string.agree_content_sixth).toString()));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", "drawable", "com.github.postsanf.skinby")));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", "color", "com.github.postsanf.skinby")));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", "color", "com.github.postsanf.skinby")));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.getInstance().mActivitys.remove(this);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
